#ifndef USER_APP
#define USER_APP
/* ----------------------- Modbus includes ----------------------------------*/
#include "mb.h"
#include "mb_m.h"
#include "mbconfig.h"
#include "mbframe.h"
#include "mbutils.h"
#include "sys_conf.h"

#define MAX_DISCRETE_REG_CNT 10

/* -----------------------Slave Defines -------------------------------------*/
#define S_REG_HOLDING_START     0
#define S_REG_HOLDING_NREGS     75
#define REG_HOLDING_WRITE_NREGS 5  //可写范围+
//从机模式：在保持寄存器中，各个地址对应的功能定义
#define S_HD_RESERVE         0  //保留
#define S_HD_CPU_USAGE_MAJOR 1  //当前CPU利用率的整数位
#define S_HD_CPU_USAGE_MINOR 2  //当前CPU利用率的小数位

//从机模式：在输入寄存器中，各个地址对应的功能定义
#define S_IN_RESERVE 0  //保留

/* -----------------------Master Defines -------------------------------------*/
#define M_REG_HOLDING_START      0
#define M_REG_HOLDING_NREGS      100
#define M_REG_HOLDING_USR_START  20
#define M_REG_HOLDING_USR2_START 0
#define M_REG_HOLDING_USR2_NREGS 3

#define M_REG_INPUT_START 0
#define M_REG_INPUT_NREGS 100
#define usMRegInStart     M_REG_INPUT_START

//主机模式：在保持寄存器中，各个地址对应的功能定义
#define M_HD_RESERVE 0  //保留

//主机模式：在输入寄存器中，各个地址对应的功能定义
#define M_IN_RESERVE 0  //保留

//主机模式：在线圈中，各个地址对应的功能定义
#define M_CO_RESERVE 2  //保留

//主机模式：在离散输入中，各个地址对应的功能定义
#define M_DI_RESERVE 1  //保留

enum {
    MBM_DEV_IPM_ADDR          = 0,   // return air temp&hum board 1 modbus address
    MB_MASTER_TOTAL_SLAVE_NUM = 16,  // Total
};
#define MB_MASTER_DISCRETE_OFFSET MBM_DEV_IPM_ADDR  // 8

// TH地址
#define MB_DEV_TH_SUPPLY_START 0  // 1
#define MB_DEV_TH_SUPPLY_END   0  // 1

#define MB_DEV_TH_RETURN_START 1  // 2
#define MB_DEV_TH_RETURN_END   1  // 2

#define M_REG_TH_INPUT_START 0
#define M_REG_TH_INPUT_NREGS 2  //输入寄存器个数
// hum&temp control board reg definition
#define MBM_DEV_TH_REG_TEMP_ADDR     0
#define MBM_DEV_TH_REG_HUMIDITY_ADDR 1

/* -----------------------Master01 Defines ------------------------------*/
#define M_REG_HOLDING_START_01 30001
#define M_REG_HOLDING_NREGS_01 66
#define M_REG_COLS_START_01    10001
#define M_REG_COLS_NREGS_01    116
enum {
    MBM_DEV_AC_ADDR = 0,     // AC modbus address
    MB_MASTER_SLAVE_NUM_01,  // Total
};

#define MBM_REG_ADDR_CNT_ADDR 0

enum {
    MBM_FSM_IDLE = 0x00,
    MBM_FSM_SYNC,
    MBM_FSM_UPDATE,
    MBM_FSM_SEND,
    MBM_FSM_GSM,
};

//
#define MBM_POLL_TIMEOUT_THRESHOLD   5
#define MBM_INIT_TIMEOUT_THRESHOLD   5
#define MBM_UPDATE_TIMEOUT_THRESHOLD 5

typedef struct {
    uint16_t poll;
    uint16_t init;
    uint16_t update;
    uint16_t discrete;
} mbm_bitmap_st;

typedef struct {
    uint16_t poll;
    uint16_t init;
    uint16_t update;
} mbm_timeout_st;

typedef struct {
    uint16_t poll;
    uint16_t init;
    uint16_t update;
} mbm_errcnt_st;
/****************************************************************
mem_dev_reg数据说明：
dev_reg_addr 设备寄存器实际地址
mem_addr 设备对应的内存映射地址
mem_addr & dev_reg_addr 必须一一对应
***************************************************************/

typedef enum {
    MB_WRITE_SINGLE,   //写单个寄存器
    MB_WRITE_MULITE,   //写多个寄存器
    MB_WR_SINGLE_BIT,  //写单个位
} eMBWriteMode;

typedef struct {
    uint16_t  dev_reg_addr;
    uint16_t  NRegs;
    uint16_t *data_ptr;
    uint16_t  J_Offset_Addr;  //跳变内存地址
} mbm_read_dev_reg;

typedef struct {
    uint16_t     dev_reg_addr;
    uint16_t     Conf_addr;
    uint16_t    *conf_ptr;
    uint8_t     *conf_Flag;
    uint16_t    *status_ptr;
    eMBWriteMode funCode;
} mbm_write_dev_reg;

typedef struct {
    uint8_t            mbm_addr;
    uint8_t            reg_rd_cnt;
    mbm_read_dev_reg  *rd_pt;
    uint8_t            reg_w_cnt;
    mbm_write_dev_reg *w_pt;
} mbm_read_st;

typedef struct {
    mbm_bitmap_st  bitmap;
    mbm_errcnt_st  errcnt;
    mbm_timeout_st timeout;
    uint16_t       mbm_fsm;
    //		uint8_t Write_Enable;
} mbm_dev_st;

typedef struct {
    uint8_t  mbm_addr;
    uint16_t reg_addr;
    uint16_t reg_value;
    uint8_t  mbm_NRegs;
    uint8_t  mbm_WriteType;
} mbm_data_st;

void mbm_sts_update(sys_reg_st *gds_ptr, UCHAR ucMB_Number);
void mbs_sts_update(void);  // 更新本地变量到协议栈寄存器中
#endif
